﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using AirLine.Entities;
using AirLine.DataAccess;

namespace AirLine.Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    [ServiceBehavior (InstanceContextMode= InstanceContextMode.PerSession)]
    public class AirLineService : IAirLineService
    {

        #region Initialization

        private IRepository _repository;
        private bool isLogin;

        public AirLineService()
        {
            _repository = new Repository(new AirLineEntities());
        }

        public AirLineService(IRepository repository)
        {
            this._repository = repository;
        }

        public bool SignIn(string username, string password)
        {
            if (username == "a" && password == "a")
                isLogin = true;
            isLogin = false;
            return isLogin;
        }

        #endregion


        #region Airport

        public IEnumerable<Airport> GetAllAirport()
        {
            IEnumerable<Airport> p = _repository.GetAll<Airport>();
            return _repository.GetAll<Airport>();
        }

        public IEnumerable<Airport> SearchAirport(string keyWord, int type)
        {
            if (keyWord != "")
            {
                switch (type)
                {
                    case 1:
                        return _repository.Get<Airport>(x => x.AirportCode == keyWord);
                    case 2:
                        return _repository.Get<Airport>(x => x.Name.Contains(keyWord));
                    case 3:
                        return _repository.Get<Airport>(x => x.CityName.Contains(keyWord));
                    case 4:
                        return _repository.Get<Airport>(x => x.Country.Contains(keyWord));
                    default:
                        return _repository.GetAll<Airport>();
                }
            }
            return _repository.GetAll<Airport>();
        }

        public bool InsertAirport(Airport newAirport)
        {
            return _repository.Add<Airport>(newAirport);
        }

        public bool UpdateAirport(Airport newAirport)
        {
            return _repository.Update<Airport>(newAirport);
        }

        public bool DeletetAirport(string code)
        {
            Airport airport = SearchAirport(code, 1).SingleOrDefault();
            if (airport == null)
                return false;
            return _repository.Delete<Airport>(airport);

        }

        #endregion

        #region Ticket Class
        public IEnumerable<TicketClass> GetAllTicketClass()
        {            
            return _repository.GetAll<TicketClass>();
        }

        public IEnumerable<SeatClass> GetAllSeatClass()
        {
            return _repository.GetAll<SeatClass>();
        }
        #endregion

        #region Flight

        #endregion

        #region Public Service
        public List<ISanBay> TimSanBayDi()
        {
            IEnumerable<V_AirportFrom> airportfrom = _repository.GetAll<V_AirportFrom>();
            List<ISanBay> sanbays = new List<ISanBay>();
            foreach (var item in airportfrom)
            {
                ISanBay sanbay = new ISanBay
                {
                    MaSanBay = item.AirportCode,
                    QuocGia = item.Country,
                    TenSanBay = item.Name,
                    ThanhPho = item.CityName
                };
                sanbays.Add(sanbay);
            }
            return sanbays;
        }

        public List<ISanBay> TimSanBayDen()
        {
            IEnumerable<V_AirportTo> airportto = _repository.GetAll<V_AirportTo>();
            List<ISanBay> sanbays = new List<ISanBay>();
            foreach (var item in airportto)
            {
                ISanBay sanbay = new ISanBay
                {
                    MaSanBay = item.AirportCode,
                    QuocGia = item.Country,
                    TenSanBay = item.Name,
                    ThanhPho = item.CityName
                };
                sanbays.Add(sanbay);
            }
            return sanbays;
        }

        public List<ILoaiVe> TimLoaiVe()
        {
            var ticketclass = _repository.GetAll<TicketClass>();
            List<ILoaiVe> loaives = new List<ILoaiVe>();
            foreach (var item in ticketclass)
            {
                ILoaiVe loaive = new ILoaiVe
                {
                    MaLoai = item.Id.ToString(),
                    TenLoai = item.Name,
                    Gia = item.Price.Value,
                    DuocPhepHuy = item.IsCancel.Value,
                    PhiHuyTruocNgayKhoiHanh = item.CancelBeforeDepartureCost.Value,
                    PhiHuySauNgayKhoiHanh = item.CancelAfterDepartureCost.Value,
                    DuocPhepDoiLichTrinh = item.IsChangeTrip.Value,
                    PhiDoiLichTrinhTruocNgayKhoiHanh = item.ChangeTripBeforeDepartureCost.Value,
                    PhiDoiLichTrinhSauNgayKhoiHanh = item.CancelAfterDepartureCost.Value,
                    DuocPhepNangHang = item.IsUpgradeClass.Value,
                    PhiNangHangTruocNgayKhoiHanh = item.UpgradeClassBeforeDeparture.Value,
                    PhiNangHangSauNgayKhoiHanh = item.UpgradeClassAfterDeparture.Value,
                    HeSoCongDam = item.GLPAccrualRates.Value,
                    ThoiHanSuDung = item.MaximumStay.Value,
                    DonViTien = "USD"
                };
                loaives.Add(loaive);                
            }
            return loaives;

        }

        public List<IChuyenBay> TimChuyenBay(string sanBayDi, string sanBayDen, DateTime thoiGianDi, DateTime thoiGianVe, string loaiVe)
        {
            List<IChuyenBay> chuyenbays = new List<IChuyenBay>();
            List<V_Flight> flights = new List<V_Flight>();
            if (loaiVe == "mot chieu")
            {
                flights = _repository.Get<V_Flight>(x => x.AirportCodeFrom == sanBayDi 
                                                    && x.AirportCodeTo == sanBayDen 
                                                    && x.DepartureDate.Value.Date == thoiGianDi.Date
                                                    && x.ArrivalDate.Value.Date == thoiGianVe.Date).ToList();
            }
            else if (loaiVe == "khu hoi")
            {
               flights = _repository.GetAll<V_Flight>().ToList();
            }
            else
            {
               flights = _repository.GetAll<V_Flight>().ToList();
            }

            foreach(var item in flights)
            {
                IChuyenBay chuyenbay = new IChuyenBay
                {
                    MaSanBayDen = item.AirportCodeFrom,
                    MaSanBayDi = item.AirportCodeTo,
                    ThoiGianDi = item.DepartureDate.Value,
                    ThoiGianVe = item.ArrivalDate.Value
                };
            }
             return chuyenbays;

        }

        }
        #endregion


    }
}
